package com.aygxy.carproject.controller;


import com.aygxy.carproject.pojo.user.dto.UserAddDTO;
import com.aygxy.carproject.pojo.user.dto.UserLoginInfoDTO;
import com.aygxy.carproject.pojo.user.dto.UserUpdateDTO;
import com.aygxy.carproject.pojo.user.vo.UserListItemVO;
import com.aygxy.carproject.service.UserService;
import com.aygxy.carproject.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/users")
@Api(tags = "01. 用户管理模块")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 100)
    @PostMapping("/login")
    public JsonResult<String> login(UserLoginInfoDTO userLoginInfoDTO) {
        log.debug("开始处理【登录认证】的请求，参数：{}", userLoginInfoDTO);
        String jwt = userService.login(userLoginInfoDTO);
        log.debug("即将向客户端响应JWT数据：{}", jwt);
        return JsonResult.ok(jwt);
    }


    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 200)
    @GetMapping("/list")
    public JsonResult<List<UserListItemVO>> list() {
        log.debug("开始处理【查询用户列表】的请求……");
        List<UserListItemVO> list = userService.list();
        return JsonResult.ok(list);
    }
    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 300)
    @GetMapping("/addNew")
    public JsonResult<Void> insert(UserAddDTO userAddDTO) {
        userService.insert(userAddDTO);
        return JsonResult.ok();
    }
    @ApiOperation(value = "修改用户基本资料", notes = "请求头中必须携带名为Authorization的JWT，且对应的用户必须具有相关权限")
    @ApiOperationSupport(order = 301)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/info/update")
    public JsonResult<Void> updateInfoById(@PathVariable Long id, UserUpdateDTO userUpdateDTO) {
        log.debug("开始处理【修改用户基本资料】的请求，id={}，新基本资料={}", userUpdateDTO);
        userService.updateInfoById(id, userUpdateDTO);
        return JsonResult.ok();
    }
    @ApiOperation(value = "根据id删除用户", notes = "请求头中必须携带名为Authorization的JWT，且对应的用户必须具有相关权限")
    @ApiOperationSupport(order = 302)
    @PreAuthorize("hasAuthority('car/admin/delete')")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        log.debug("开始处理【根据id删除用户】的请求：id={}", id);
        userService.deleteById(id);
        return JsonResult.ok();
    }
    @ApiOperation(value = "启用用户账号", notes = "请求头中必须携带名为Authorization的JWT，且对应的用户必须具有相关权限")
    @ApiOperationSupport(order = 310)
    @PreAuthorize("hasAuthority('car/admin/update')")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用用户账号】的请求：id={}", id);
        userService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation(value = "禁用用户账号", notes = "请求头中必须携带名为Authorization的JWT，且对应的用户必须具有相关权限")
    @ApiOperationSupport(order = 311)
    @PreAuthorize("hasAuthority('car/admin/update')")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用用户账号】的请求：id={}", id);
        userService.setDisable(id);
        return JsonResult.ok();
    }








}
